package com.lupan.tomcatStudy.jndi.service;

import com.lupan.tomcatStudy.jndi.common.EmailUtil;
import com.lupan.tomcatStudy.jndi.domain.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.mail.MessagingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 说明：JNDI
 *
 * @author lupan
 * @version 2016/6/27 12:48
 */
public class JNDIService {

    private Logger logger = LoggerFactory.getLogger(JNDIService.class);

    private JNDIService() {

    }

    private static volatile JNDIService singleton;

    public static JNDIService getInstance() {
        if (singleton == null) {
            synchronized (JNDIService.class) {
                if (singleton == null) {
                    singleton = new JNDIService();
                }
            }
            return singleton;
        } else {
            return singleton;
        }
    }


    /**
     * 发送邮件
     * @param addresses to邮箱地址
     * @param subject 主题
     * @param content 内容
     */
    public void sendEmail(String[] addresses, String subject, String content) {
        try {
            EmailUtil.sendEmail(addresses,subject,content);
        } catch (NamingException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        } catch (MessagingException e) {
            System.out.println(e.toString());
            e.printStackTrace();
        }
    }

    /**
     * 通过JNDI获取dataSource
     */
    public Student getDataSource(){
        //获取JNDI资源
        Context initCtx;
        DataSource dataSource = null;
        try {
            initCtx = new InitialContext();
            dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/mysql/test");

        } catch (NamingException e) {
            e.printStackTrace();
        }
        JdbcTemplate template = new JdbcTemplate(dataSource);
        Student student = template.queryForObject("select * from student where id = ?", new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                Student result = new Student();
                result.setId(rs.getLong("id"));
                result.setName(rs.getString("name"));
                result.setNum(rs.getString("num"));
                return result;
            }
        },1);
        logger.info(student.toString());
        return student;
    }

    /**
     * 通过jndi来获取资源
     * @return 资源
     */
    public com.lupan.bean.Student getBeanByJNDI() {
        com.lupan.bean.Student student = null;
        try {
            Context initCtx = new InitialContext();
            student = (com.lupan.bean.Student) initCtx.lookup("java:comp/env/bean/studentBean");
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return student;
    }
}
